package geocoding;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;

import org.xml.sax.SAXException;

import com.keithpower.gekmlib.KMLParser;
import com.keithpower.gekmlib.Kml;
import com.keithpower.gekmlib.Point;

public class Geocoder {
	public static void main(String [] args) {
		try {
			String line;
			int stopNum = 0;
			BufferedReader f = new BufferedReader(new FileReader(args[0]));
			
            while ((line = f.readLine()) != null) {
            	String data = line;
            	
            	System.out.println(data);
            	System.out.println("http://maps.google.com/maps/geo?q="+URLEncoder.encode(data,"UTF-8")+"&output=xml&key=ABQIAAAAgByTI66yzMFQhME1QKHI5xQ4I7b9GqOYj-8--pk-lKjLg5zpGRQik5CIywkp-DW4FzOyw_TdEWkR_w");
            	Kml kmlRoot = new KMLParser().parse(new InputStreamReader(new URL("http://maps.google.com/maps/geo?q="+URLEncoder.encode(data,"UTF-8")+"&output=xml&key=ABQIAAAAgByTI66yzMFQhME1QKHI5xQ4I7b9GqOYj-8--pk-lKjLg5zpGRQik5CIywkp-DW4FzOyw_TdEWkR_w").openStream()));
            	if (kmlRoot.getPlacemark() != null) System.out.println(((Point)kmlRoot.getPlacemark().getGeometry()).getCoordinates());
            	else System.out.println("keine GPS-Koordinaten");
            }
		}
		catch (IOException e) {
			System.out.println(e);
		}
		catch (SAXException e) {
			System.out.println(e);
		}
	}
}
